#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "red_juego.h"
#include "red_socketcliente.h"
#include "red_hilodeentrada.h"
#include "red_imsgsal.h"
#include "red_msgsalchat.h"
#include <cstdlib>
#include <string>
#include <sstream>
#include <iostream>

using namespace red;
using namespace red::sockets;
using namespace red::hilos;
using namespace red::protocolo::mensajes::salida;
using namespace std;

int main(int argc, char *argv[]) {

	Socket *t_pSocket = NULL;
	Juego *t_pJuego;
	HiloDeEntrada *t_pHiloDeEntrada;
	string t_sTexto;
	IMsgSal *t_pMsgSal;
	Protocolo *t_pProtocolo = NULL;

	t_pSocket = new SocketCliente("200.126.231.245", 15553);

	if (!t_pSocket) {
	} else if (!t_pSocket->getUtilizable()) {
		delete t_pSocket;
	} else {

		t_pJuego = new Juego(t_pSocket);
		t_pHiloDeEntrada = new HiloDeEntrada(t_pSocket, *t_pJuego);
		t_pHiloDeEntrada->start();

		t_pProtocolo = new Protocolo(t_pSocket);

		while (t_sTexto != "quit") {
			t_sTexto = "";
			getline(cin, t_sTexto);
			t_pMsgSal = new MsgSalChat(t_sTexto);
			t_pProtocolo->bEnviarMensaje(t_pMsgSal);
			delete t_pMsgSal;
		}

		delete t_pProtocolo;

		t_pHiloDeEntrada->terminar();
		delete t_pHiloDeEntrada;
		delete t_pJuego;
		delete t_pSocket;
	}

	return 0;
}
